package drrr.orm;

import com.opensymphony.xwork2.ActionContext;
import drrr.common.SpringContext;

import javax.persistence.*;
import javax.persistence.Entity;
import java.sql.Timestamp;
import java.util.Calendar;

/**
 * Created by lirance on 15/5/19.
 */
@Entity
@Table(name = "room", schema = "", catalog = "drrr")
public class RoomEntity extends drrr.orm.Entity {
    private int id;
    private Timestamp createDate;
    private Integer type;
    private String roomname;
    private Integer membernum;
    private Integer owner;
   // private Integer onlinNum;

    @Basic
    @Column(name = "owner")
    public Integer getOwner() {
        return owner;
    }

    public void setOwner(Integer owner) {
        this.owner = owner;
    }

    @Id
    @Column(name = "id")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Basic
    @Column(name = "createDate")
    public Timestamp getCreateDate() {
        if(createDate==null)
        {
            createDate = new Timestamp(System.currentTimeMillis());
        }
        return createDate;
    }

    public void setCreateDate(Timestamp createDate) {
        this.createDate = createDate;
    }

    @Basic
    @Column(name = "type")
    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    /**
     * 获取房间类型参数
     * 请忽略这个错误
     * @return 房间类型字符串
     */
    public String getTypeString() {
        switch (type) {
            case 1:
                return "长期";
            case 2:
                return "临时";
            default:
                return "错误";
        }
    }

    /**
     * 获取用户实体
     * 请忽略错误
     * @return
     */
    public UserEntity getOwnerUserEntity()
    {
        return ((AllService)SpringContext.getBean("allService")).getUserService().getUserEntity(getOwner());
    }

    /**
     * 获取房间在线人数
     * 请忽略这个错误
     * @return 房间在线人数字符串
     */
    public String getOnlineNum() {

        String roomOnline = String.format("roomOnline%s",this.getId()+"");
        String onlineNum;
        try {
            onlineNum = ActionContext.getContext().getApplication().get(roomOnline).toString();
        }catch (Exception e){
            onlineNum = "0";
        }

        return onlineNum;

    }

    @Basic
    @Column(name = "roomname")
    public String getRoomname() {
        return roomname;
    }

    public void setRoomname(String roomname) {
        this.roomname = roomname;
    }

    @Basic
    @Column(name = "membernum")
    public Integer getMembernum() {
        return membernum;
    }

    public void setMembernum(Integer membernum) {
        this.membernum = membernum;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        RoomEntity that = (RoomEntity) o;

        if (id != that.id) return false;
        if (createDate != null ? !createDate.equals(that.createDate) : that.createDate != null) return false;
        if (type != null ? !type.equals(that.type) : that.type != null) return false;
        if (roomname != null ? !roomname.equals(that.roomname) : that.roomname != null) return false;
        if (membernum != null ? !membernum.equals(that.membernum) : that.membernum != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (createDate != null ? createDate.hashCode() : 0);
        result = 31 * result + (type != null ? type.hashCode() : 0);
        result = 31 * result + (roomname != null ? roomname.hashCode() : 0);
        result = 31 * result + (membernum != null ? membernum.hashCode() : 0);
        return result;
    }

}
